package com.hr.interview.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import com.hr.LandR.domain.LeadershipRelationship;
import com.hr.LandR.mapper.LeadershipRelationshipMapper;
import com.hr.borrowing.domain.Borrowing;
import com.hr.borrowing.mapper.BorrowingMapper;
import com.hr.common.core.domain.entity.SysUser;
import com.hr.common.utils.DateUtils;
import com.hr.interview.domain.Interview;
import com.hr.jobTimeRecord.domain.JobTimeRecord;
import com.hr.jobTimeRecord.mapper.JobTimeRecordMapper;
import com.hr.system.domain.SysUserPost;
import com.hr.system.domain.SysUserRole;
import com.hr.system.mapper.SysUserMapper;
import com.hr.system.mapper.SysUserPostMapper;
import com.hr.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.hr.interview.mapper.InterviewMapper;
import com.hr.interview.domain.InterviewDTO;
import com.hr.interview.service.IInterviewService;
import com.hr.prompt.Prompt;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 查看面试信息Service业务层处理
 * 
 * @author guan
 * @date 2024-07-11
 */
@Service
public class InterviewServiceImpl implements IInterviewService 
{
    @Resource
    private InterviewMapper interviewMapper;

    @Resource
    private SysUserMapper userMapper;

    @Resource
    private SysUserPostMapper userPostMapper;

    @Resource
    private JobTimeRecordMapper jobTimeRecordMapper;

    @Resource
    private BorrowingMapper borrowingMapper;

    @Resource
    private SysUserRoleMapper userRoleMapper;

    @Resource
    private LeadershipRelationshipMapper LRMapper;

    /**
     * 查询查看面试信息
     * 
     * @param interviewId 查看面试信息主键
     * @return 查看面试信息
     */
    @Override
    public Interview selectInterviewByInterviewId(Long interviewId)
    {
        return interviewMapper.selectInterviewByInterviewId(interviewId);
    }

    /**
     * 查询查看面试信息列表
     * 
     * @param interview 查看面试信息
     * @return 查看面试信息
     */
    @Override
    public List<Interview> selectInterviewListDef(Interview interview)
    {
        return interviewMapper.selectInterviewListDef(interview);
    }

    /**
     * 默认查询查看面试信息列表
     *
     * @param interview 查看面试信息
     * @return 查看面试信息
     */
    @Override
    public List<Interview> selectInterviewList(Interview interview)
    {
        return interviewMapper.selectInterviewList(interview);
    }

    /**
     * 新增查看面试信息
     * 
     * @param interview 查看面试信息
     * @return 结果
     */
    @Override
    public int insertInterview(Interview interview)
    {

//        根据userId判断用户是否已经入职
        if (interviewMapper.selectUserByIdId(interview.getUserId()) == 1){
            return 0;
        }
        interview.setCreateTime(DateUtils.getNowDate());
        return interviewMapper.insertInterview(interview);
    }

    /**
     * 修改查看面试信息
     * 
     * @param interviewDTO 查看面试信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateInterview(InterviewDTO interviewDTO)
    {
        Long userId = interviewDTO.getUserId();
        Integer userIdleState = interviewDTO.getUserIdleState();

        //更新求职者状态
        SysUser user = new SysUser();
        user.setUserId(userId);
        user.setIdleState(userIdleState);
        userMapper.updateUser(user);

//        检查前端传来的数据没有遗漏
        String cardNumber = interviewDTO.getCardNumber();
        String staffNumber = interviewDTO.getStaffNumber();
        List<Long> postId = interviewDTO.getPostId();
        Integer flag = interviewDTO.getFlag();
        String nation = interviewDTO.getNation();
        Long deptId = interviewDTO.getDeptId();
        Long roleId = interviewDTO.getRoleId();
        BigDecimal wages = interviewDTO.getWages();
        if ( wages != null && roleId != null && deptId != null && postId != null
                && flag != null && cardNumber != null && staffNumber != null && nation != null){
//            补充用户入职数据
            user.setCardNumber(cardNumber);
            user.setStaffNumber(staffNumber);
            user.setFlag(flag);
            user.setNation(nation);
            user.setDeptId(deptId);
            user.setWages(wages);
            user.setRoleId(roleId);
            userMapper.updateSysUser(user);
//            分配员工岗位
            List<SysUserPost> list = new ArrayList<>(postId.size());
            for(Long id : postId){
                SysUserPost userPost = new SysUserPost();
                userPost.setUserId(userId);
                userPost.setPostId(id);
                list.add(userPost);
            }
            userPostMapper.batchUserPost(list);
            //修改用户角色
            SysUserRole userRole = new SysUserRole();
            userRole.setUserId(userId);
            userRole.setRoleId(roleId);
            List<SysUserRole> listUR = new ArrayList<>();
            listUR.add(userRole);
            userRoleMapper.batchUserRole(listUR);
//            记录入职时间
            JobTimeRecord jobTimeRecord = new JobTimeRecord();
            jobTimeRecord.setUserId(userId);
            jobTimeRecord.setEntryTime(DateUtils.getNowDate());
            jobTimeRecord.setFlag(Prompt.staff_status_Active);
            jobTimeRecordMapper.insertJobTimeRecord(jobTimeRecord);
//            修改借支权限信息
            Borrowing borrowing = new Borrowing();
            borrowing.setUserId(userId);
            borrowing.setAmount(interviewDTO.getAmount());
            borrowing.setNumber(interviewDTO.getNumber());
//            判断借支权限flag，若为“1”则往借支表插入数据
            if (flag.equals("1") && borrowingMapper.selectBorrowingByUserId(userId) != null){
                borrowingMapper.updateBorrowing(borrowing);
            }else if (flag.equals("2") && borrowingMapper.selectBorrowingByUserId(userId) != null){
//                若为“2”且有借支权限，则取消该用户的借支权限
                borrowingMapper.deleteBorrowingByUserId(userId);
            }else {
                borrowingMapper.insertBorrowing(borrowing);
            }
        }
//        设置领导人关系
        Long leaderId = userMapper.selectUserIdByNickName(interviewDTO.getLeaderNickName());
        LeadershipRelationship leadershipRelationship = new LeadershipRelationship(userId,leaderId);
        LRMapper.updateLeadershipRelationship(leadershipRelationship);
//        修改面试信息
        Interview interview = new Interview();
        interview.setUserIdleState(userIdleState);
        interview.setUserId(userId);
        interview.setUpdateTime(DateUtils.getNowDate());
        interview.setInterviewId(interviewDTO.getInterviewId());
        return interviewMapper.updateInterview(interview);
    }

    /**
     * 批量删除查看面试信息
     * 
     * @param interviewIds 需要删除的查看面试信息主键
     * @return 结果
     */
    @Override
    public int deleteInterviewByInterviewIds(Long[] interviewIds)
    {
        return interviewMapper.deleteInterviewByInterviewIds(interviewIds);
    }

    /**
     * 删除查看面试信息信息
     * 
     * @param interviewId 查看面试信息主键
     * @return 结果
     */
    @Override
    public int deleteInterviewByInterviewId(Long interviewId)
    {
        return interviewMapper.deleteInterviewByInterviewId(interviewId);
    }
}
